<?php
class PressAction extends CommonAction {

    public function index() {
        $news = D('News');
        $nav = M('PressNav');
        $navs = $news->field('nav')->select();
        $navArr = array();
        foreach($navs as $key=>$value) {
            $navName = $nav->where('id='.$value['nav'])->getField('name');
            $navArr[$navName] = $value['nav'];
        }
        parent::search(array(
            'n.title'=>'新闻标题',
            'n.nav'=>'新闻栏目',
            'n.attr'=>'新闻属性'
        ), array(
            'n.nav'=>$navArr
        ));
        if(!isset($_GET['act'])) {
            parent::page($news->count());
            $allPress = $news->alias('n')
                             ->field(array('n.id', 'n.title', 'n.title'=>'ftitle', 'n.nav', 'n.attr', 'n.is_commend' ,'pn.name'))
                             ->join('xinxi_press_nav pn ON n.nav=pn.id')
                             ->limit($this->limit)
                             ->order('n.nav ASC,n.addtime DESC,n.is_commend DESC')
                             ->select();
         } else if($_GET['act'] == 'search') {
             parent::page($news->alias('n')->where($this->map)->count());
             $allPress = $news->alias('n')
                              ->field(array('n.id', 'n.title', 'n.title'=>'ftitle', 'n.nav', 'n.attr', 'n.is_commend' ,'pn.name'))
                              ->join('xinxi_press_nav pn ON n.nav=pn.id')
                              ->where($this->map)
                              ->limit($this->limit)
                              ->order('n.nav ASC,n.addtime DESC,n.is_commend DESC')
                              ->select();
         }
        $this->assign('allpress', $allPress);
        $this->assign('empty', '<tr><td colspan="6">没有任何数据</td></tr>');
        $this->display();
    }

    public function add() {
        $pn = D('PressNav');
        if(isset($_POST['send'])) {
            $news = D('News');
            if($news->create()) {
                $news->add() ? $this->success('新增新闻成功!', __URL__) : $this->error('系统出错，新增新闻失败!', 0);
            } else {
                $this->error($news->getError(), 0);
            }
        }
        $this->assign('allnav', $pn->field(array('id', 'name'))->where('is_active=1')->select());
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $news = D('News');
            $pn = D('PressNav');
            if(isset($_POST['send'])) {
                if($news->create()) {
                    $mem = parent::useMemcached();
                    $mem->delete("newdetails-$id");
                    $news->save() ? $this->success('修改新闻成功!', __URL__) : $this->error('系统出错，新增新闻失败!', 0);
                } else {
                    $this->error($news->getError(), 0);
                }
            }
            $press = $news->field(array('id', 'title', 'nav', 'attr', 'thumbnail', 'is_commend', 'content', 'from', 'writer' ,'summary', 'view_count'))->where('id='.$id)->find();
            $this->assign('allnav', $pn->field(array('id', 'name'))->where('is_active=1')->select());
            $this->assign('press', $press);
            $this->display();
        }
    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $news = D('News');
            if(!$news->where('id='.$id)->getField('id')) $this->error('不存在此数据', 0);
            $news->where('id='.$id)->delete() ? $this->success('删除新闻成功!', __URL__) : $this->error('系统出错，删除新闻失败!', 0);
        }
    }

    public function mulDelete() {
        $news = D('News');
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请至少选择一项!', 0);
        $map['id'] = array('in', $ids);
        $news->where($map)->delete() ? $this->success('批量删除新闻成功!', __URL__) : $this->error('系统出错，批量删除新闻失败!', 0);
    }
}